import Compiler from './compiler.js';
import Observer from './observe.js';

class Vue {
    // 构造器方法
    constructor(options) {
        this.$el = options.el;
        this.$data = options.data || {};
        // 处理数据
        this._proxyData(options.data);
        // 处理方法
        this._proxyMethods(options.methods);
        // 劫持数据
        new Observer(this.$data);
        // 执行模板编译
        this.compiler = new Compiler(this);
    }


    // 劫持data中的数据
    _proxyData(data) {
        Object.keys(data).forEach(key => {
            Object.defineProperty(this, key, {
                get() {
                    return data[key];
                },
                set(newVal) {
                    data[key] = newVal;
                }
            });
        });
        /* const obj = new Proxy(data, {
            get(target, value) {
                return Reflect.get(target, value);
            },
            set(target, key, value) {
                return Reflect.set(target, key, value);
            },
            deleteProperty(target, key) {
                // 将删除操作反射到原始对象上
                return Reflect.deleteProperty(target, key);
            }
        });
        Object.keys(obj).forEach(key => {
            this[key] = obj[key];
        })
        */
    }

    // 将methods直接挂在到this对象上
    _proxyMethods(methods) {
        Object.keys(methods).forEach(key => {
            if (methods[key]) {
                this[key] = methods[key];
            }
        });
    }
}

window.Vue = Vue;

export default Vue;